//
//  SLMineViewController.m
//  StarLight
//
//  Created by 吴涛 on 16/5/9.
//  Copyright © 2016年 Duke.Wu. All rights reserved.
//

#import "SLMineViewController.h"
#import "SLShipOwnerVipLevelView.h"
#import "SLUserManager.h"
#import "SLMineViewModel.h"
#import "SLMoreSetViewController.h"
#import "SLMessageCenterViewController.h"
#import "SLBackListViewController.h"
#import "SLVIPJoinViewController.h"
#import "SLChangeHeadeViewController.h"




@interface SLMineViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *choicesTableView;

@property (nonatomic, strong) UIImageView *userIcon;

@property (nonatomic, strong) UILabel *userNameLabel;

@property (nonatomic, strong) UILabel *userTypeLabel;

@property (nonatomic, strong) SLShipOwnerVipLevelView *levelView;

@property (nonatomic, strong) UILabel *vipLabel;

@property (nonatomic, strong) NSMutableArray *dataSource;

@property (nonatomic, strong) SLMineViewModel *mineViewModel;

@end

@implementation SLMineViewController


static NSString *messageCount = @"1";

- (void)viewDidLoad {
    [super viewDidLoad];
  
    // Do any additional setup after loading the view.
  [self setUpMineUI];
  [self addConstrains];
  [self addObservers];
}

- (void)viewWillAppear:(BOOL)animated{
  [super viewWillAppear:animated];
//  [self refreshMine];
}


- (void)addObservers{
  @weakify(self)
  [RACObserve(self.mineViewModel, indexModel) subscribeNext:^(SLMineUserIndexModel *model) {
    @strongify(self)
    if (model) {
      [self configSelfWithIndexModel:model];
    }
  
  }];
  // 更换头像后 刷新页面
  [[[NSNotificationCenter defaultCenter] rac_addObserverForName:kChangeHeadSuccess object:nil] subscribeNext:^(NSNotification *notify) {
    @strongify(self)
    [self refreshMine];
    self.userIcon.image = notify.userInfo[@"userIcon"];
  }];
  
  // 会员付款成功后 刷新页面
  [[[NSNotificationCenter defaultCenter] rac_addObserverForName:kJoinVipSucess object:nil] subscribeNext:^(id x) {
    
    [self refreshMine];
  }];
  
}

- (void)refreshMine{
  [self.mineViewModel getUserindexCallBack:^(id respond, NSError *error) {
    [self configSelfWithIndexModel:self.mineViewModel.indexModel];
  }];
}


static NSInteger userType;

// 数据更新的时候 更新 UI
- (void)configSelfWithIndexModel:(SLMineUserIndexModel *)model{
  
  userType = model.type;
  self.userNameLabel.text = model.name;
  self.userTypeLabel.text = model.type == 0 ? @"船主" : @"货主";
  [self.userIcon sd_setImageWithURL:[NSURL URLWithString:model.headPhoto] placeholderImage:[UIImage imageNamed:@"head portrait"]];
  messageCount = [NSString stringWithFormat:@"%ld",(long)model.mesnum];
  
  if (model.role < 1) {
    [self.levelView removeFromSuperview];
    [self.vipLabel removeFromSuperview];
  }else{
    [self.view addSubview:self.vipLabel];
    [self.view addSubview:self.levelView];
    [self.levelView configWithLevel:[NSString stringWithFormat:@"%ld",(long)model.role]];
    [self.levelView mas_remakeConstraints:^(MASConstraintMaker *make) {
      make.centerX.equalTo(self.userIcon.mas_right);
      make.top.equalTo(self.userNameLabel.mas_bottom).offset(8);
      make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    
    [self.vipLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
      make.centerY.equalTo(self.levelView);
      make.right.equalTo(self.levelView.mas_left).offset(-5);
    }];
    [self.view layoutIfNeeded];
  }
  
  
  [self.choicesTableView reloadData];

}


#pragma mark - setup UI

- (void)setUpMineUI{
  UIImageView *topBackImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg"]];
  topBackImageView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 250*MIX_H);
  [self.view addSubview:topBackImageView];
  [self.view addSubview:self.userIcon];
  [self.view addSubview:self.userNameLabel];
  [self.view addSubview:self.userTypeLabel];
  [self.view addSubview:self.vipLabel];
  [self.view addSubview:self.levelView];
  [self.view addSubview:self.choicesTableView];
  
  
  [self.choicesTableView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(topBackImageView.mas_bottom);
    make.width.equalTo(self.view);
    make.centerX.equalTo(self.view);
    make.bottom.equalTo(self.mas_bottomLayoutGuideTop);
  }];
  
  
  
  
}

- (void)addConstrains{
  [self.userIcon mas_makeConstraints:^(MASConstraintMaker *make) {
    make.centerX.equalTo(self.view);
    make.top.equalTo(self.view).offset(86*MIX_H);
    make.size.mas_equalTo(CGSizeMake(60*MIX_W, 60*MIX_W));
  }];
  
  [self.userNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.centerX.equalTo(self.view);
    make.top.equalTo(self.userIcon.mas_bottom).offset(14);
  }];
  
  [self.levelView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.centerX.equalTo(self.userIcon.mas_right);
    make.top.equalTo(self.userNameLabel.mas_bottom).offset(8);
    make.size.mas_equalTo(CGSizeMake(20, 20));
  }];
  
  [self.vipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.centerY.equalTo(self.levelView);
    make.right.equalTo(self.levelView.mas_left).offset(-5);
  }];
  
  [self.userTypeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.userNameLabel.mas_bottom).offset(8);
    make.centerX.equalTo(self.vipLabel.mas_left).offset(-20.f);
    make.centerX.equalTo(self.view).priority(250);
  }];
  
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  return 5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  return 59;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  
  
  if(indexPath.row == 0){
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell1"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.font = [UIFont systemFontOfSize:16];
    cell.imageView.image = [UIImage imageNamed:self.dataSource[0][@"image"]];
    cell.textLabel.text = self.dataSource[0][@"text"];
    cell.detailTextLabel.text = self.mineViewModel.indexModel.time;
    cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
    return cell;
  }else if (indexPath.row == 1) {
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell0"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.imageView.image = [UIImage imageNamed:@"Message push icon"];
    cell.textLabel.text = @"我的消息";
    cell.textLabel.font = [UIFont systemFontOfSize:16];
    cell.detailTextLabel.textColor = SLYellowColor;
    cell.detailTextLabel.text = messageCount;
    return cell;
  }else{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell1"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.font = [UIFont systemFontOfSize:16];
    cell.imageView.image = [UIImage imageNamed:self.dataSource[indexPath.row-1][@"image"]];
    cell.textLabel.text = self.dataSource[indexPath.row-1][@"text"];
    return cell;
  }
  
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  if (indexPath.row == 4) {
    [SLUserManager loginOut];
  }
  
  if (indexPath.row == 3) {
    [self showBackMsgListVC];
  }
  
  if (indexPath.row == 2) {
    SLMoreSetViewController *moreSetVC = [[SLMoreSetViewController alloc] init];
    [self.tabBarController.navigationController pushViewController:moreSetVC animated:YES];
  }
  
  if (indexPath.row == 1) {
    [self showMessageCenter];
  }
  
  if (indexPath.row == 0) {
    [self showVIP];
  }
  
  
}

- (void)showVIP{
  SLVIPJoinViewController *vipVC = [SLVIPJoinViewController new];
  vipVC.userType = userType;
  vipVC.memberImage = self.userIcon.image;
  [self.tabBarController.navigationController pushViewController:vipVC animated:YES];
}

- (void)showBackMsgListVC{
  SLBackListViewController *backListVC = [SLBackListViewController new];
  [self.tabBarController.navigationController pushViewController:backListVC animated:YES];
}

- (void)showMessageCenter{
  SLMessageCenterViewController *msgCenterVC = [SLMessageCenterViewController new];
  [self.tabBarController.navigationController pushViewController:msgCenterVC animated:YES];
}

- (void)showChangeHead{
  SLChangeHeadeViewController *changeVC = [SLChangeHeadeViewController new];
  changeVC.oldHead = self.userIcon.image;
  [self.tabBarController.navigationController pushViewController:changeVC animated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - getter

- (NSMutableArray *)dataSource{
  if (!_dataSource) {
    _dataSource = [@[@{@"text":@"VIP办理",@"image":@"VIP"},@{@"text":@"个人设置",@"image":@"Personal settings"},@{@"text":@"意见反馈",@"image":@"Feedback"},@{@"text":@"退出登录",@"image":@"sign out"}  ] mutableCopy];
    
    
  }
  return _dataSource;
}


- (SLShipOwnerVipLevelView *)levelView{
  if (!_levelView) {
    _levelView = [SLShipOwnerVipLevelView new];
  }
  return _levelView;
}

-(UILabel *)vipLabel{
  if (!_vipLabel) {
    _vipLabel = [UILabel labelwithBoldFontSize:15 textColor:[UIColor redColor] text:@"VIP"];
  }
  return _vipLabel;
}

- (UILabel *)userTypeLabel{
  if (!_userTypeLabel) {
    _userTypeLabel = [UILabel labelwithBoldFontSize:14 textColor:[UIColor whiteColor] text:@"用户类型"];
  }
  return _userTypeLabel;
}

- (UILabel *)userNameLabel{
  if (!_userNameLabel) {
    _userNameLabel = [UILabel labelwithBoldFontSize:18 textColor:[UIColor whiteColor] text:@"用户名"];
  }
  return _userNameLabel;
}

- (UIImageView *)userIcon{
  if (!_userIcon) {
    _userIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"head portrait"]];
    _userIcon.layer.borderColor = SLYellowColor.CGColor;
    _userIcon.layer.borderWidth = 1;
    _userIcon.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapges = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showChangeHead)];
    [_userIcon addGestureRecognizer:tapges];
  }
  return _userIcon;
}


- (UITableView *)choicesTableView{
  if (!_choicesTableView) {
    _choicesTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    [_choicesTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    _choicesTableView.delegate = self;
    _choicesTableView.dataSource = self;
    _choicesTableView.tableFooterView = [UIView new];
  }
  return _choicesTableView;
}

- (SLMineViewModel *)mineViewModel{
  if (!_mineViewModel) {
    _mineViewModel = [SLMineViewModel shareInstance];
  }
  return _mineViewModel;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
